<?php

namespace App\Jobs;

use App\Movie;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

use Illuminate\Support\Facades\Log;
use Symfony\Component\DomCrawler\Crawler;
use GuzzleHttp\Client;
use App\Image;

class UpdateMovieInfo extends Job implements ShouldQueue
{
    use InteractsWithQueue, SerializesModels;
    protected $movie;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(Movie $movie)
    {
        $this->movie = $movie;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        if ($this->attempts() > 2) {
            $this->delete();
            return;
        }
        //http://www.zmz2017.com/search/index?keyword=game+of+throne&search_type=
        $base_url = config('app.yyets_base_url');
        $header_params = [
            'Host' => 'http://www.zmz2017.com',
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'Accept-Encoding' => 'gzip, deflate, sdch',
            'Accept-Language' => 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
            'Cache-control:' => 'max-age=0',
            'Dnt' => '1',
            'Referer' => 'https://www.baidu.com/link?url=OOIVn0qbApPkoe99TFbeGI307BTpCTY4pXGJcll69PEQRzlZjqbxMmnq9DoI3AWQ&wd=&eqid=fe299461002fe7600000000557314a9e',
            'Upgrade-Insecure-Requests' => '1',
            'User-Agent' => 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Mobile Safari/537.36',
        ];
        //获取登陆cookie
        $client = new Client([
            'headers' => $header_params,
            'cookies' => true,
            'allow_redirects' => true,
            'base_uri' => $base_url
        ]);
        $show_key_name = $this->show->name_en;
        $res = $client->request('GET', '/search/index', ['query' => ['keyword' => $show_key_name]]);
        $dom = $res->getBody()->getContents();
        $crawler = new Crawler($dom);

        $show_cate_name = $crawler->filter('body > div.middle-box > div > div.area-left > div > div.search-result > ul > li > div > em')->first()->text();

        if ($show_cate_name != '电影') {
            Log::info('YYETS搜索 电影无结果:' . $this->movie->name_en);
            return;
        }

        $search_result_url = $crawler->filter('body > div.middle-box > div > div.area-left > div > div.search-result > ul > li > div > div.fl-info > div > a')->first();
        if ($search_result_url->count() < 1) {
            Log::info('YYETS搜索电影不到' . $this->movie->name_en);
            return;
        } else {

            $search_result_url = $search_result_url->attr('href');

            $detail_dom = $client->request('GET', $search_result_url)->getBody()->getContents();
            $crawler = new Crawler($detail_dom);

            $detail_text_selector = 'body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.resource-desc > div:nth-child(3)';
            $coverage_selector = 'body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.clearfix.resource-con > div.fl-img > div.imglink > a > img';
            $key_name_selector = 'body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.clearfix.resource-con > div.fl-info > ul > li.mg';
            $play_writer_selector = 'body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.clearfix.resource-con > div.fl-info > ul > li:nth-child(9)';
            $direcotr_selector = 'body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.clearfix.resource-con > div.fl-info > ul > li:nth-child(10)';
            $actor_selector = 'body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.clearfix.resource-con > div.fl-info > ul > li.rel';
            $type_sel = 'body > div.middle-box > div > div.box.clearfix.res-view-top > div.fl.view-left > div.clearfix.resource-con > div.fl-info > ul > li:nth-child(6)';

            $node = $crawler->filter($detail_text_selector);
            if ($node->count()) {
                $detail_text = $node->first()->html();
            }
            $image_link = $crawler->filter($coverage_selector)->first()->attr('src');
            $key_name = $crawler->filter($key_name_selector)->first()->text();
            $play_writer = $crawler->filter($play_writer_selector)->first()->text();
            $director = $crawler->filter($direcotr_selector)->first()->text();
            $actor = $crawler->filter($actor_selector)->first()->text();
            $type = $crawler->filter($type_sel)->first()->text();

            $this->movie->detail = $detail_text;
            $this->movie->key_word = $key_name;
            $this->movie->type = $type;

            $this->movie->play_writer = $play_writer;
            $this->movie->director = $director;
            $this->movie->actor = $actor;

            dd($this->movie->toArray());
            $this->movie->save();

            $key = $this->movie->name_en . date('y_md_His', time());
            $alt = $this->movie->name_en . $this->show->name_zh;
            $image = Image::fetchUrlWithKey($image_link, $key, $alt);
            $this->show->images()->save($image);
        }
    }
}
